package com.springboot.shanhexin.dao;

import com.springboot.shanhexin.entity.hotel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "hotelDao")
public interface hotelDao {

    //旅店：hotel

    /**
     * 显示总数量
     * @return
     */
    @Select("select count(*) from hotel")
    int getCount();

    /**
     * 全部分页显示
     * @return
     */
    @Select("select a.*,b.name as p_name from hotel a,place b where a.pid=b.id limit #{pos},#{size}")
    List<hotel> findByPage(@Param("pos")int pos, @Param("size")int size);

    /**
     * 显示搜索的总数量
     * @return
     */
    @Select("select count(*) from hotel a,place b where a.pid=b.id and ${key} like concat(concat('%',#{name}),'%')")
    int getsearchCount(@Param("key")String key,@Param("name")String name);

    /**
     * 搜索并分页显示
     * @return
     */
    @Select("select a.*,b.name as p_name from hotel a,place b where a.pid=b.id and" +
            " ${key} like concat(concat('%',#{name}),'%') limit #{pos},#{size}")
    List<hotel> findsearchByPage(@Param("key")String key,@Param("name")String name,@Param("pos")int pos, @Param("size")int size);

    /**
     * 新增
     */
    @Insert("INSERT INTO hotel(pid,name,address,time,price,phone,introduce,img) " +
            "VALUES(#{pid},#{name},#{address},#{time},#{price},#{phone},#{introduce},#{img})")
    void add(hotel hotel);

    /**
     * 删除
     */
    @Delete("DELETE FROM hotel WHERE ID=#{id}")
    void delete(@Param("id")int id);

    /**
     * 修改(先找到并显示，再修改)
     */
    @Select("select a.*,b.name as p_name from hotel a,place b where a.pid=b.id and a.id=#{id}")
    List<hotel> getUserone(@Param("id")int id);

    @Update("update hotel set pid=#{pid},name=#{name},address=#{address},time=#{time},price=#{price}," +
            "phone=#{phone},introduce=#{introduce},img=#{img} where id=#{id}")
    void update(hotel hotel);

}
